package ubo.master1.jeupoker.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Afichage action des utilisateurs, 
 * Le tapi , les mises 
 *   
 */
public class BoardPanel extends JPanel {

    private static final int NO_OF_CARDS = 5;
    /** The control panel. */
    private final ControlPanel controlPanel;
    /** Label with the bet. */
    private final JLabel betLabel;
    /** Label with the pot. */
    private final JLabel potLabel;
    /** Labels with the community cards. */
    private final JLabel[] cardLabels;
    /** Label with a custom message. */
    private final JLabel messageLabel;

    /**
     * Constructor.
     * 
     * @param mainFrame
     *            The main frame.
     */
    public BoardPanel(ControlPanel controlPanel) {
        this.controlPanel = controlPanel;
        this.setOpaque(false);
        // setBorder(UIConfiguration.PANEL_BORDER);
        setBackground(UIConfiguration.TABLE_COLOR);
        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel label = new JLabel("Bet");
        label.setForeground(Color.GREEN);
        gc.gridx = 1;
        gc.gridy = 0;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.NONE;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 0, 5);
        add(label, gc);

        label = new JLabel("Pot");
        label.setForeground(Color.GREEN);
        gc.gridx = 3;
        gc.gridy = 0;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.NONE;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 0, 5);
        add(label, gc);

        betLabel = new JLabel();
        betLabel.setBorder(UIConfiguration.LABEL_BORDER);
        betLabel.setForeground(Color.GREEN);
        betLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 1;
        gc.gridy = 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 5, 5);
        add(betLabel, gc);

        potLabel = new JLabel();
        potLabel.setBorder(UIConfiguration.LABEL_BORDER);
        potLabel.setForeground(Color.GREEN);
        potLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 3;
        gc.gridy = 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 5, 5);
        add(potLabel, gc);

        // The five card positions.
        cardLabels = new JLabel[NO_OF_CARDS];
        for (int i = 0; i < 5; i++) {
            cardLabels[i] = new JLabel(AcceeCarts.getIcon("/images/card_back.png"));
            gc.gridx = i;
            gc.gridy = 2;
            gc.gridwidth = 1;
            gc.gridheight = 1;
            gc.weightx = 0.0;
            gc.weighty = 0.0;
            gc.anchor = GridBagConstraints.CENTER;
            gc.fill = GridBagConstraints.NONE;
            gc.insets = new Insets(10, 5, 10, 5);
            add(cardLabels[i], gc);
        }

        // Message label.
        messageLabel = new JLabel();
        messageLabel.setForeground(Color.YELLOW);
        messageLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 0;
        gc.gridy = 3;
        gc.gridwidth = 5;
        gc.gridheight = 1;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.insets = new Insets(10, 0, 5, 0);
        add(messageLabel, gc);

        // Control panel.
        gc.gridx = 0;
        gc.gridy = 4;
        gc.gridwidth = 5;
        gc.gridheight = 1;
        gc.weightx = 1.0;
        gc.weighty = 1.0;
        gc.insets = new Insets(5, 0, 5, 0);
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.BOTH;
        add(controlPanel, gc);
        // Initialisation des carte lors du premier affichage 
        update(null, 0, 0);
    }

    /**
     * Updates the current hand status.
     * 
     * @param bet
     *            The bet.
     * @param pot
     *            The pot.
     */
    public void update(List<LaCart> cards, int bet, int pot) {
        if (bet == 0) {
            betLabel.setText("10");
        } else {
            betLabel.setText("$ " + bet);
        }
        if (pot == 0) {
            potLabel.setText(" 100");
        } else {
            potLabel.setText("$ " + pot);
        }
        int noOfCards = (cards == null) ? 0 : cards.size();
        for (int i = 0; i < NO_OF_CARDS; i++) {
            if (i < noOfCards) {
                cardLabels[i].setIcon(AcceeCarts.getCardImage(cards.get(i)));
            } else {
                cardLabels[i].setIcon(AcceeCarts.getIcon("/images/card_back.png"));
            }
        }
        //Juste pour remplir le tapi 
        cardLabels[0].setIcon(AcceeCarts.getIcon("/images/card_48.png"));
        cardLabels[1].setIcon(AcceeCarts.getIcon("/images/card_49.png"));
        cardLabels[2].setIcon(AcceeCarts.getIcon("/images/card_50.png"));
    }
}
